package com.anolesoft.yr.mbus.prod.model.crtl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.prod.base.dto.ProdBaseServiceSaveDTO;
import com.anolesoft.yr.mbus.prod.base.dto.ProdBaseServiceUpdateDTO;
import com.anolesoft.yr.mbus.prod.base.entity.ProdBaseService;
import com.anolesoft.yr.mbus.prod.base.service.ProdBaseServiceService;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceQueryNameVO;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceQueryVO;
import com.anolesoft.yr.mbus.prod.model.dto.*;
import com.anolesoft.yr.mbus.prod.model.entity.ProdProduction;
import com.anolesoft.yr.mbus.prod.model.entity.ProdType;
import com.anolesoft.yr.mbus.prod.model.service.ProdProductionService;
import com.anolesoft.yr.mbus.prod.model.service.ProdTypeService;
import com.anolesoft.yr.mbus.prod.model.vo.ProdProductionQueryVO;
import com.anolesoft.yr.mbus.prod.model.vo.ProdServiceQueryVO;
import com.anolesoft.yr.mbus.prod.model.vo.ProdServiceValueQueryVO;
import com.anolesoft.yr.mbus.prod.model.vo.ProdTypeQueryVO;
import com.anolesoft.yr.mbus.util.GlobalLoginInfo;
import com.anolesoft.yr.mbus.util.StrCP;
import com.anolesoft.yr.util.PageInfo;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@CrossOrigin
@RequestMapping(value = "/prodProduction")
public class ProdProductionController {

    @Autowired
    ProdProductionService prodProductionService;
    @Autowired
    ProdTypeService prodTypeService;
    @Autowired
    ProdBaseServiceService prodBaseServiceService;

    //新增产品信息表
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveProdProduction(@RequestBody ProdType prodType) throws Exception {
        String prodProductionUid = StrCP.createObjuid32(UUID.randomUUID().toString());

        List prodBaseServiceSaveDTOList = Lists.newArrayList();
        List<ProdBaseService> prodBaseServiceList = prodType.getProdBaseService();
        for (int i = 0; i < prodBaseServiceList.size(); i++) {
            ProdBaseService prodBaseService = prodBaseServiceList.get(i);
            ProdBaseServiceSaveDTO prodBaseServiceSaveDTO = BeanUtil.copyProperties(prodBaseService, ProdBaseServiceSaveDTO.class);
            prodBaseServiceSaveDTO.setObjuid(prodBaseService.getObjuid());
            prodBaseServiceSaveDTOList.add(prodBaseServiceSaveDTO);
        }

        List<ProdProduction> prodProductionList = prodType.getProdProduction();
        ProdProduction prodProduction = prodProductionList.get(0);
        ProdProductionSaveDTO  prodProductionSaveDTO = BeanUtil.copyProperties(prodProduction, ProdProductionSaveDTO.class);
        prodProductionSaveDTO.setObjuid(prodProductionUid);
        prodProductionSaveDTO.setFacCode("");
        prodProductionSaveDTO.setProdTypeUid(prodType.getObjuid());
        prodProductionSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prodProductionSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
        prodProductionSaveDTO.setDelFlag(0);
        prodProductionSaveDTO.setProdBaseServiceSaveDTO(prodBaseServiceSaveDTOList);

        String str = "";
        try {
            str = prodProductionService.saveProdProduction(prodProductionSaveDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        String jsons = "";
        jsons += "[{";
        jsons += "\"str\"" + ":" + "\"" + str + "\"";
        jsons += "}]";

        return new AjaxResult(200, str);
    }

    //修改产品信息表
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateProdProduction(@RequestBody ProdType prodType) throws Exception {
        List prodBaseServiceUpdateDTOList = Lists.newArrayList();
        List<ProdBaseService> prodBaseServiceList = prodType.getProdBaseService();
        for (int i = 0; i < prodBaseServiceList.size(); i++) {
            ProdBaseService prodBaseService = prodBaseServiceList.get(i);
            ProdBaseServiceUpdateDTO prodBaseServiceUpdateDTO = BeanUtil.copyProperties(prodBaseService, ProdBaseServiceUpdateDTO.class);
            prodBaseServiceUpdateDTO.setObjuid(prodBaseService.getObjuid());
            prodBaseServiceUpdateDTO.setUpdateObjuid(prodBaseService.getUpdateObjuid());
            prodBaseServiceUpdateDTOList.add(prodBaseServiceUpdateDTO);
        }

        List<ProdProduction> prodProductionList = prodType.getProdProduction();
        ProdProduction prodProduction = prodProductionList.get(0);
        ProdProductionUpdateDTO prodProductionUpdateDTO = BeanUtil.copyProperties(prodProduction, ProdProductionUpdateDTO.class);
        prodProductionUpdateDTO.setProdTypeUid(prodType.getObjuid());
        prodProductionUpdateDTO.setMmUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prodProductionUpdateDTO.setMmTime(GlobalLoginInfo.getGlobalLoginTime());
        prodProductionUpdateDTO.setProdBaseServiceUpdateDTO(prodBaseServiceUpdateDTOList);

        String str = "";
        try {
            str = prodProductionService.updateProdProduction(prodProductionUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        String jsons = "";
        jsons += "[{";
        jsons += "\"str\"" + ":" + "\"" + str + "\"";
        jsons += "}]";

        return new AjaxResult(200, str);
    }


    //根据主键查询基础服务项信息
    @RequestMapping(value = "/queryBaseServiceByUid", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryBaseServiceByUid(@RequestBody ProdServiceQueryDTO prodServiceQueryDTO) throws Exception {
        Map<String, Object> maps = prodBaseServiceService.queryProdBaseServiceByUid(prodServiceQueryDTO.getObjuid());
        ProdBaseServiceQueryVO prodBaseServiceQueryVO = (ProdBaseServiceQueryVO) maps.get("prodBaseServiceQueryVO");

        String jsons = "";
        if (prodBaseServiceQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prodBaseServiceQueryVO);
            jsons += "]";
        }

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //查询基础服务项名称
    @RequestMapping(value = "/queryBaseServiceName", method = RequestMethod.POST)
    @ResponseBody
    public String queryBaseServiceName() throws Exception {
        List prodBaseServiceQueryNameVOList = prodBaseServiceService.queryBaseServiceName();

        String jsons = "";
        jsons += "[";
        if (prodBaseServiceQueryNameVOList.size() > 0) {
            for (int i = 0; i < prodBaseServiceQueryNameVOList.size(); i++) {
                ProdBaseServiceQueryNameVO prodBaseServiceQueryNameVO = (ProdBaseServiceQueryNameVO) prodBaseServiceQueryNameVOList.get(i);
                jsons += new Gson().toJson(prodBaseServiceQueryNameVO);
                if (i < (prodBaseServiceQueryNameVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return jsons;
    }

    //删除产品信息表
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deleteProdProduction(@RequestBody List<ProdProduction> prodProductionList) throws Exception {
        List prodProductionDeleteDTOList = new ArrayList();
        ProdProductionDeleteDTO prodProductionDeleteDTO = null;

        for (int i = 0; i < prodProductionList.size(); i++) {
            ProdProduction prodProduction = prodProductionList.get(i);

            prodProductionDeleteDTO = new ProdProductionDeleteDTO();
            prodProductionDeleteDTO.setObjuid(prodProduction.getObjuid());
            prodProductionDeleteDTOList.add(prodProductionDeleteDTO);
        }

        String str = "";
        try {
            str = prodProductionService.deleteProdProduction(prodProductionDeleteDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //查询产品类型名称
    @RequestMapping(value = "/queryTypeName", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdTypeName() throws Exception {
        List prodTypeQueryVOList = prodTypeService.queryProdTypeName();

        String jsons = "";
        jsons += "[";
        if (prodTypeQueryVOList.size() > 0) {
            for (int i = 0; i < prodTypeQueryVOList.size(); i++) {
                ProdTypeQueryVO prodTypeQueryVO = (ProdTypeQueryVO) prodTypeQueryVOList.get(i);
                jsons += new Gson().toJson(prodTypeQueryVO);
                if (i < (prodTypeQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据主键查询产品及服务项数据
    @RequestMapping(value = "/queryByUid", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdProductionByUid(@RequestBody ProdProductionQueryDTO prodProductionQueryDTO) throws Exception {
        Map<String, Object> maps = prodProductionService.queryProdProductionByUId(prodProductionQueryDTO.getObjuid());
        ProdProductionQueryVO prodProductionQueryVO = (ProdProductionQueryVO) maps.get("prodProductionQueryVO");
        List prodServiceQueryVOList = (List) maps.get("prodServiceQueryVOList");

        String jsons = "";
        jsons += "{";
        jsons += "\"prodProduction\":";
        if (prodProductionQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prodProductionQueryVO);
            jsons += "]";
        }
        jsons += ",\"prodService\":";
        jsons += "[";
        if (prodServiceQueryVOList.size() > 0) {
            for (int i = 0; i < prodServiceQueryVOList.size(); i++) {
                ProdServiceQueryVO prodServiceQueryVO = (ProdServiceQueryVO) prodServiceQueryVOList.get(i);
                jsons += new Gson().toJson(prodServiceQueryVO);
                if (i < (prodServiceQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";
        jsons += "}";

        System.out.println("jsons：" + jsons);
        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据主键查询产品服务项及参数数据
    @RequestMapping(value = "/queryByUid2", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdServiceByUid(@RequestBody ProdServiceQueryDTO prodServiceQueryDTO) throws Exception {
        Map<String, Object> maps = prodProductionService.queryProdServiceByUid(prodServiceQueryDTO.getObjuid());
        ProdServiceQueryVO prodServiceQueryVO = (ProdServiceQueryVO) maps.get("prodServiceQueryVO");
        List prodServiceValueQueryVOList = (List) maps.get("prodServiceValueQueryVOList");

        String jsons = "";
        jsons += "{";
        jsons += "\"prodService\":";
        if (prodServiceQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prodServiceQueryVO);
            jsons += "]";
        }
        jsons += ",\"prodServiceValue\":";
        jsons += "[";
        if (prodServiceValueQueryVOList.size() > 0) {
            for (int i = 0; i < prodServiceValueQueryVOList.size(); i++) {
                ProdServiceValueQueryVO prodServiceValueQueryVO = (ProdServiceValueQueryVO) prodServiceValueQueryVOList.get(i);
                System.out.println(prodServiceValueQueryVO);
                jsons += new Gson().toJson(prodServiceValueQueryVO);
                if (i < (prodServiceValueQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";
        jsons += "}";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据条件查询产品所有数据
    @RequestMapping(value = "/queryAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdProductionAll(String typeName, String prodName, int page, int rows) throws Exception {
        Map maps = prodProductionService.queryProdProductionAll(typeName, prodName, page, rows);
        int count = (int) maps.get("count");
        List prodProductionQueryVOList = (List) maps.get("prodProductionQueryVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prodProductionQueryVOList));
    }

    //查询产品名称
    @RequestMapping(value = "/queryProdName", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdProductionProdName() throws Exception {
        List prodProductionQueryVOList = prodProductionService.queryProdProductionProdName();

        String jsons = "";
        jsons += "[";
        if (prodProductionQueryVOList.size() > 0) {
            for (int i = 0; i < prodProductionQueryVOList.size(); i++) {
                ProdProductionQueryVO prodProductionQueryVO = (ProdProductionQueryVO) prodProductionQueryVOList.get(i);
                jsons += new Gson().toJson(prodProductionQueryVO);
                if (i < (prodProductionQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

}
